#include <iostream>
#include "chatserver.hpp"
#include "chatservice.hpp"
#include <signal.h>
#include "friendmodel.hpp"

#include "mysql_conn_pool.hpp"

using std::cout;
using std::endl;

#define PORT    6969
#define IP      "127.0.0.1"

//数据库连接池初始化相关参数
#define SQL_URL             "127.0.0.1"
#define SQL_PORT            3306
#define SQL_USER            "root"
#define SQL_PASSWORD        "61053208"
#define SQL_DATABASE        "chat"
#define SQL_POOL_MAX_CONN   5


void resetHandler(int){
    ChatService::getInstance()->reset();
    exit(0);
}

int main(int argc,char* argv[]){
    //服务器启动时，将所有用户的状态改为离线
    //ChatService::getInstance()->reset();

    //获取传进来的服务器地址和端口
    if(argc<3){
        std::cerr<<"command invalid! example: ./ChatServer 127.0.0.1 6969 "<<endl;
        exit(-1);
    }
    char* arg_ip=argv[1];   //ip
    uint16_t arg_port=atoi(argv[2]); //port

    signal(SIGINT,resetHandler);//设置监听服务器退出事件

    //数据库连接池初始化
    if(mysql_connPool::getInstance()->init(SQL_URL,SQL_USER,SQL_PASSWORD,SQL_DATABASE,SQL_PORT,SQL_POOL_MAX_CONN)==false){
        exit(-1);
    }


    EventLoop loop;

    //设置服务器ip和端口
    InetAddress addr(arg_ip,arg_port);

    //创建利用了muduo库的聊天服务器对象
    ChatServer server(&loop,addr,"ChatServer");

    //启动服务器
    server.start();

    //启动主循环事件
    loop.loop();

    return 0;
}